package com.ctrl.riveradministrator.mvp.view.activity.bulletin;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.base.BaseViewHolder;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.Bulletin;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class BulletinAdapter extends BaseListAdapter<Bulletin> {

    private Context context;
    private TextView bulletinName, phone, people;
    private ImageView levelIv;
    private RelativeLayout content;
    private SimpleDraweeView imageView;
    private GenericDraweeHierarchyBuilder builder;
    private GenericDraweeHierarchy hierarchy;
    private ImageRequest request;
    private DraweeController controller;

    public BulletinAdapter(Context mContext) {
        super(mContext);
        this.context = mContext;
        builder = new GenericDraweeHierarchyBuilder(context.getResources());
    }

    @Override
    public int getLayoutId() {
        return R.layout.bullentin_item;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindItemHolder(BaseViewHolder holder, int position) {
        levelIv = holder.getView(R.id.level_iv);
        bulletinName = holder.getView(R.id.bulletin_name_tv);
        phone = holder.getView(R.id.phone_tv);
        people = holder.getView(R.id.people_tv);
        imageView = holder.getView(R.id.bulletin_iv);
        content = holder.getView(R.id.content);

        Bulletin bulletin = getDataList().get(position);
        if (bulletin.getGradeId() == null) {
            levelIv.setImageDrawable(null);
        } else {
            switch (bulletin.getGradeId()) {
                case "1":
                    levelIv.setImageDrawable(context.getDrawable(R.drawable.sheng));
                    break;
                case "2":
                    levelIv.setImageDrawable(context.getDrawable(R.drawable.shi));
                    break;
                case "3":
                    levelIv.setImageDrawable(context.getDrawable(R.drawable.xian));
                    break;
                case "4":
                    levelIv.setImageDrawable(context.getDrawable(R.drawable.cun));
                    break;
            }
        }
        bulletinName.setText(bulletin.getPsignName());
        phone.setText(bulletin.getTelephone());
        people.setText(bulletin.getContacts());
        content.setOnClickListener(v -> mOnItemClickListener.onItemClick(bulletin, position));
        Uri uri = Uri.parse(Constants.BASE_URL_ + bulletin.getPath());
        hierarchy = imageView.getHierarchy();
        hierarchy.setFailureImage(R.mipmap.ic_launcher);
        hierarchy.setProgressBarImage(new ProgressBarDrawable());
        //imageView.setImageURI(uri);
        if (bulletin.getPath() == null || bulletin.getPath().equals("")) {
            return;
        }
        request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(100, 100))
                .build();
        controller = Fresco.newDraweeControllerBuilder()
                .setOldController(imageView.getController())
                .setImageRequest(request)
                .build();
        imageView.setController(controller);
    }


}
